package com.ap.core.webservice.manageconsumer;

import com.ap.core.business.message.email.handler.EmailHandler;
import com.ap.core.cxf.jaxb.manageconsumer.RequestRegisterConsumerType;
import com.ap.core.cxf.jaxb.manageconsumer.RequestRetrieveConsumerDetailsType;
import com.ap.core.cxf.jaxb.manageconsumer.ResponseRegisterConsumerType;
import com.ap.core.cxf.jaxb.manageconsumer.ResponseRetrieveConsumerDetailsType;
import com.ap.core.exception.BusinessFailureException;
import com.ap.core.message.info.BaseMessageInfo;
import com.ap.core.message.info.RequestMessageInfo;
import com.ap.core.message.service.CoreResponseInfo;
import com.ap.core.webservice.BaseWSService;
import com.core.ap.wsdl.manageconsumerservice_1.ManageConsumer1PortType;
import com.core.ap.wsdl.manageconsumerservice_1.RegisterConsumer1Fault;
import com.core.ap.wsdl.manageconsumerservice_1.RetrieveConsumerDetails1Fault;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;


/**
 * Implementation class for {@link com.core.ap.wsdl.manageconsumerservice_1.ManageConsumer1PortType}
 */
public class ManageConsumerWebServiceImpl extends BaseWSService<RequestMessageInfo, BaseMessageInfo, String> implements ManageConsumer1PortType {

    private EmailHandler<BaseMessageInfo> emailHandler;

    @Override
    public ResponseRegisterConsumerType registerConsumer1(@WebParam(partName = "registerConsumer_1Request", name = "RegisterConsumer_1Request", targetNamespace = "http://ap.core.com/xsd/manageconsumerdata_1") RequestRegisterConsumerType registerConsumer1Request) throws RegisterConsumer1Fault {
        CoreResponseInfo<BaseMessageInfo, String> coreResponseInfo = null;
        LOG.info("Starting processing in ap core at time: " + System.currentTimeMillis() + " for message type = " + registerConsumer1Request.getMsgHdr().getMsgType().name());
        try {
            coreResponseInfo = messageProcessor.process(registerConsumer1Request, new ResponseRegisterConsumerType());
        } catch (BusinessFailureException e) {
            throw new RegisterConsumer1Fault(buildMessage(registerConsumer1Request.getMsgHdr(), e));
        } catch (RuntimeException e) {
            LOG.error("Runtime exception occurred while registering consumer", e);
            LOG.info("As LDAP doesn't support transaction so it might be possible that there is orphan record in LDAP schema for user = " + registerConsumer1Request.getRegistrationInfo().getCredentials().getEmailId());
            LOG.info("This orphan record needs to be deleted manually by operation from LDAP");
            throw new RegisterConsumer1Fault("Runtime exception occurred while registering consumer", e);
        }
        try{
            emailHandler.handleEmailRequest(registerConsumer1Request, coreResponseInfo.getAdditionalInfo());
        }catch (RuntimeException e) {
            LOG.error("Runtime exception occurred while sending email request", e);
            throw new RegisterConsumer1Fault("Runtime exception occurred while sending email request", e);
        }


        LOG.info("Finishing processing in ap core at time: " + System.currentTimeMillis() + " for message type = " + registerConsumer1Request.getMsgHdr().getMsgType().name());
        return (ResponseRegisterConsumerType) coreResponseInfo.getResponse();
    }

	@Override
	@WebResult(name = "RetrieveConsumerDetails_1Response", targetNamespace = "http://ap.core.com/xsd/manageconsumerdata_1", partName = "retrieveConsumerDetails_1Response")
	@WebMethod(operationName = "retrieveConsumerDetails_1", action = "retrieveConsumerDetails_1")
	public ResponseRetrieveConsumerDetailsType retrieveConsumerDetails1(
			@WebParam(partName = "retrieveConsumerDetails_1Request", name = "RetrieveConsumerDetails_1Request", targetNamespace = "http://ap.core.com/xsd/manageconsumerdata_1") RequestRetrieveConsumerDetailsType retrieveConsumerDetails1Request)
			throws RetrieveConsumerDetails1Fault {
         CoreResponseInfo<BaseMessageInfo, String> coreResponseInfo = null;
		 LOG.info("Starting processing in ap core at time: " + System.currentTimeMillis() + " for message type = " + retrieveConsumerDetails1Request.getMsgHdr().getMsgType().name());
		 try {
             coreResponseInfo = messageProcessor.process(retrieveConsumerDetails1Request, new ResponseRetrieveConsumerDetailsType());
			 LOG.info("Finishing processing in ap core at time: " + System.currentTimeMillis() + " for message type = " + retrieveConsumerDetails1Request.getMsgHdr().getMsgType().name());
			 return (ResponseRetrieveConsumerDetailsType)coreResponseInfo.getResponse();
		 } catch(BusinessFailureException e) {
			 throw new RetrieveConsumerDetails1Fault(buildMessage(retrieveConsumerDetails1Request.getMsgHdr(), e));
		 }
	}

    public void setEmailHandler(EmailHandler<BaseMessageInfo> emailHandler) {
        this.emailHandler = emailHandler;
    }
}
